/**
 * @ Author: luoqi
 * @ Create Time: 2024-07-26 23:45
 * @ Modified by: luoqi
 * @ Modified time: 2024-10-12 00:42
 * @ Description:
 */

#include <bsp.h>
#include "init.h"
#include "qshell/qterm.h"
#include "mem/flash.h"

static QCliCmd _cmd_reboot;
static int _cmd_reboot_hdl(int argc, char **argv);

static QCliCmd _cmd_ps;
static int _cmd_ps_hdl(int argc, char **argv);

static QCliCmd _cmd_ktime;
static int _cmd_ktime_hdl(int argc, char **argv);

static QCliCmd _cmd_blink;
static int _cmd_blink_hdl(int argc, char **argv);

void app_init(void)
{
    bsp_init();
    qterm_init();
    qterm_attach(&_cmd_reboot, "reboot", _cmd_reboot_hdl, "reboot system");
    qterm_attach(&_cmd_ps, "ps", _cmd_ps_hdl, "process status");
    qterm_attach(&_cmd_ktime, "ktime", _cmd_ktime_hdl, "kernel time");
    qterm_attach(&_cmd_blink, "blink", _cmd_blink_hdl, "blink commands");
    flash_init();
}

static int _cmd_ps_hdl(int argc, char **argv)
{
    
    return 0;
}

static int _cmd_reboot_hdl(int argc, char **argv)
{
    bsp_reboot();
    return 0;
}

static int _cmd_ktime_hdl(int argc, char **argv)
{
    uint64_t now = HAL_GetTick();
    uint32_t sec = now / 1e3;
    uint32_t ms = now;
    QSH(" running: %u.%u(sec)\r\n", sec, ms);
    return 0;
}

void _blink_help()
{
    QSH(" -stat <norm/recv/finish>\r\n");
}

static int _cmd_blink_hdl(int argc, char **argv)
{
    if(ISARG(argv[1], "?")) {
        _blink_help();
    } else if(ISARG(argv[1], "stat") && (argc == 3)) {
        if(ISARG(argv[2], "norm")) {
            bsp_blink_set(300, 100);
        } else if(ISARG(argv[2], "finish")) {
            bsp_blink_set(1000, 1000);
        } else if(ISARG(argv[2], "updating")) {
            bsp_blink_set(25, 25);
        } else {
            return -1;
        }
    }
    return 0;
}
